﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using outREACH.App_Code;

namespace outREACH
{
    public partial class ContactUs_NonMem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                if (Request.Cookies["User"] != null)
                    txtUser.Text = Request.Cookies["User"].Value;
                if (Request.Cookies["Pass"] != null)
                    txtPass.Text = Request.Cookies["Pass"].Value;
                if (Request.Cookies["User"] != null && Request.Cookies["Pass"] != null)
                    chkRemember.Checked = true;
            } 
        }

        protected void Login_Click(object sender, EventArgs e)
        {
            if (chkRemember.Checked == true)
            {
                Response.Cookies["User"].Value = txtUser.Text;
                Response.Cookies["Pass"].Value = txtPass.Text;
                Response.Cookies["User"].Expires = DateTime.Now.AddMonths(2);
                Response.Cookies["Pass"].Expires = DateTime.Now.AddMonths(2);
            }
            else
            {
                Response.Cookies["User"].Expires = DateTime.Now.AddMonths(-1);
                Response.Cookies["Pass"].Expires = DateTime.Now.AddMonths(-1);
            }

            Users u = Users.AuthorizeUser(txtUser.Text);
            if (txtPass.Text.ToString() == u.password.ToString())
            {
                Session["UserName"] = txtUser.Text;
                Response.Redirect("~/Education/EducationMem.aspx");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            ContactUs cu = new ContactUs();
            cu.dateTime = DateTime.Now;
            cu.firstName = TextBox7.Text.ToString();
            cu.lastName = TextBox8.Text.ToString();
            cu.company = TextBox9.Text.ToString();
            cu.email = TextBox10.Text.ToString();
            cu.phone = TextBox11.Text;
            cu.section = DropDownList2.SelectedValue.ToString();
            cu.statement = TextBox12.Text.ToString();
            ContactUs.InsertContactUs(cu);

            List<Users> result = Users.GetUser();
            string name = TextBox7.Text.ToString() + " " + TextBox7.Text.ToString();
            MailMessage message = new MailMessage();
            message.From = new MailAddress(TextBox10.Text, name);
            message.To.Add(new MailAddress("outreach360@gmail.com"));

            message.Subject = DropDownList2.Text;
            message.Body = "Dear Outreach, " + TextBox7.Text.ToString() + " " + TextBox8.Text.ToString() + " has a question to ask regarding the " + DropDownList2.SelectedValue.ToString() + " section!" + "\n" + "\n" + TextBox12.Text;

            SmtpClient smtp = new SmtpClient();

            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential("outreach360@gmail.com", "360outreach");

            smtp.Send(message);

            TextBox7.Text = "";
            TextBox8.Text = "";
            TextBox9.Text = "";
            TextBox10.Text = "";
            TextBox11.Text = "";
            TextBox12.Text = "";
            DropDownList2.ClearSelection();
            Label4.Visible = true;
        }

        protected void TextBox8_TextChanged(object sender, EventArgs e)
        {

        }
    }
}